#!/usr/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/controllers/utils/getdict.py
'''
common/get_city    获取城市列表 
common/get_province    获取省份列表 
common/get_country    获取国家列表 

Created on 2012-4-4

@author: zc
'''
from controllers.weibo.weibo import APIClient
import web
from controllers.sessionHandler import session_get 
from dbhandle.mysqldb import mysqldb 
from config.settings import CONSUME_KEY, CONSUME_SECRET, _CALLBACK_URL

class getdict:
    def GET( self ):
        session = web.config._session
        self.access_token, self.uid, self.expires_in = session_get( session )
        self.client = APIClient( app_key = CONSUME_KEY, app_secret = CONSUME_SECRET )
        if self.access_token is None:
            raise web.found( self.client.get_authorize_url( _CALLBACK_URL ) )
        self.client.set_access_token( self.access_token, self.expires_in )
        self.res = self.client.get.common__get_country()
        dbo = mysqldb()
        for c in self.res:
            country_id = c.keys()[0] 
            country = c[c.keys()[0]]
            row = dbo.insert( 'dict_country', country_id = country_id, country = country )
            if row == -1 :
                continue
            else:
                print country_id, country
                self.getprovince( country_id )
        return  len( self.res )
    
    
    def getprovince( self, country_id ):
        self.res = self.client.get.common__get_province( country = country_id )
        dbo = mysqldb()
        for c in self.res:
            province_id = c.keys()[0] 
            province = c[c.keys()[0]]
            row = dbo.insert( 'dict_province', province_id = province_id, province = province )
            if row == -1 :
                continue
            else:
                print province_id, province
                self.getcity( province_id )
            
                
    def getcity( self, province_id ):
        self.res = self.client.get.common__get_city( province = province_id )
        dbo = mysqldb()
        for c in self.res:
            city_id = c.keys()[0] 
            city = c[c.keys()[0]]
            row = dbo.insert( 'dict_city', city_id = city_id, city = city )
            if row == -1 :
                continue
    
